import {Actor, Action} from 'iflux2';
import {fromJS} from 'immutable';

import {GoodsType} from '../constant/action-type';

export default class GoodsActor extends Actor{
    defaultState() {
        return {
            goodsList: [],
            chooseGoods: []
        }
    }


    @Action(GoodsType.SET_GOODS)
    setGoods(state, list) {
        return state.set('goodsList', list);
    }


    @Action(GoodsType.SET_CHOOSE_GOODS)
    setChooseGoods(state, goods) {
        return state.update('chooseGoods', (choose)=>choose.concat(goods));
    }


    @Action(GoodsType.DELETE_CHOOSE_GOODS)
    deleteChooseGoods(state, index) {
        return state.update('chooseGoods', (chooseGoods)=>chooseGoods.delete(index));
    }


    @Action(GoodsType.CHANGE_NUM)
    changeNum(state, {index, itemAmount}) {
        return state.updateIn(['chooseGoods', index], goods=>goods.set('itemAmount', itemAmount));
    }


    @Action(GoodsType.RESET_CHOSEN_GOODS)
    resetChosen(state) {
        return state.set('chooseGoods', fromJS([]));
    }
}